JITS = {};


JITS.getCursorPosition = function(e) {
    e = e || window.event;
    var cursor = {x:0, y:0};
    if (e.pageX || e.pageY) {
        cursor.x = e.pageX;
        cursor.y = e.pageY;
    } 
    else {
        var de = document.documentElement;
        var b = document.body;
        cursor.x = e.clientX + 
            (de.scrollLeft || b.scrollLeft) - (de.clientLeft || 0);
        cursor.y = e.clientY + 
            (de.scrollTop || b.scrollTop) - (de.clientTop || 0);
    }
    return cursor;
};


JITS.rightClickMenu = {};
JITS.rightClickMenu.setup_init = function()
{
        if(! MVC.$E('menu'))
        {
	        var shadow = document.createElement('div');
	        shadow.id = 'menuShadow';
			document.body.appendChild(shadow);
			var div = document.createElement('div');
	        div.id = 'menu';
	        document.body.appendChild(div);
			
		}else
			var div = MVC.$E('menu');
		div.style.position='absolute';
	    div.style.display='none';
		
		shadow.style.position='absolute';
	    shadow.style.display='none';
		JITS.shadow = shadow;
		JITS.menu = div;
		div.onmouseover = stopPropagation;
        MVC.Event.observe(document.body, "click", JITS.rightClickMenu.clear);
        document.body.oncontextmenu = JITS.rightClickMenu.clear;
};


JITS.rightClickMenu.timeout = null;
JITS.rightClickMenu.working_element = null;

/*
    determines where to get the rightClickMenu
    sets up open to be called sometime in the future
*/
JITS.rightClickMenu.open = function(text, event)
{
    var cursor = JITS.getCursorPosition(event);
	var shadow = JITS.shadow;
	var shadow_style =  shadow.style;
    var rcm = JITS.menu;
	var rcm_style = rcm.style;
    rcm.innerHTML = text;
    rcm_style.width = '200px';
	shadow.style.width = '200px';

	var dimensions = getDimensions();
	
	if(cursor.x+300 >= dimensions.width)
    {
        rcm_style.left ='';
        rcm_style.right = (dimensions.width - cursor.x)+'px' ;
		shadow_style.left ='';
        shadow_style.right = (dimensions.width - cursor.x-2)+'px' ;
    }else
    {
        rcm_style.left = (cursor.x+3)+'px';
		shadow_style.left = (cursor.x+5)+'px';
    }
	if(cursor.y+210 > dimensions.height){
		rcm_style.top = '';
		shadow_style.top = '';
		rcm_style.bottom = (dimensions.height - cursor.y +5) +'px';
		 shadow_style.bottom = (dimensions.height - cursor.y +3) + 'px';
	}else{
		 rcm_style.top = (cursor.y+3)+'px';
		 shadow_style.top = (cursor.y+5)+'px';
		 rcm_style.bottom = '';
		 shadow_style.bottom = '';
	}
	 
	rcm_style.display = 'block';
	
    setTimeout(JITS.set_shadow_height,1);
	 
    return MVC.$E( rcm );
};
JITS.set_shadow_height = function(){
	JITS.shadow.style.height = (MVC.$E(JITS.menu).getHeight() - 6 )+'px';
	JITS.shadow.style.display = 'block';
};
JITS.rightClickMenu.clear = function(){
    MVC.$E(JITS.menu).style.display = "none";
	MVC.$E(JITS.shadow).style.display = "none";
};
